package b1.mobile.blackberry.dao;

import org.json.me.JSONArray;
import org.json.me.JSONObject;

import b1.mobile.blackberry.entity.GenericEntity;

public class ActivityEntity extends GenericEntity {

	public static String JK_CODE = "Number";
	public static String JK_TYPE = "Activity";
	public static String JK_CONTACT = "ContactPerson";
	public static String JK_REMARKS = "Remarks";
	public static String JK_CONTENT = "Content";
	public static String JK_DOCTYPE = "DocumentType";
	public static String JK_DOCNUM = "DocumentNum";
	public static String JK_BY = "Handledby";
	public static String JK_CARDNAME = "CardName";
	
	public static String JK_LINK = "LinkedInfoList";
	
	public static String JK_STARTDATE = "StartDate";
	public static String JK_ENDDATE = "EndDate";
	public static String JK_STATUS = "Status";
	
	public static String JK_STARTTIME = "StartDateTime";
	public static String JK_ENDTIME = "EndDateTime";
	public static String JK_DURATION = "Duration";
	public static String JK_DURATIONTYPE = "DurationType";
	
	public ActivityEntity(JSONObject obj){
		super(obj);
	}
	
	public String getTypeCode(){
		return getStringProperty(JK_TYPE);
	}
	
	public String getType(){
		String typeCode = getTypeCode();
		String type = "Others";
		
		if(typeCode.equals("C")){
			type = "Phone Call";
		} else if(typeCode.equals("M")){
			type = "Meeting";
		} else if(typeCode.equals("E")){
			type = "Note";
		} else if(typeCode.equals("T")){
			type = "Task";
		} 
		return type;
	}
	
	public String getDocumentDesc(){
		String desc = "<No Related Document>";
		String docType = getStringProperty(JK_DOCTYPE);
		if(!docType.equals("")){
			desc = docType + " #" + getStringProperty(JK_DOCNUM);
		}
		return desc;
	}
	
	public String getStartTime(){
		String time = getStringProperty(JK_STARTDATE);
		if(time.equals("")){
			time = getStringProperty(JK_STARTTIME);
		}
		return time;
	}
	
	public String getEndTime(){
		String time = getStringProperty(JK_ENDDATE);
		if(time.equals("")){
			time = getStringProperty(JK_ENDTIME);
		}
		return time;
	}
	
	public String getDuration(){
		return getStringProperty(JK_DURATION) + " " + getStringProperty(JK_DURATIONTYPE);
	}

	public String getBPCode() {
		String bpCode = null;
		Object obj = getProperty(JK_LINK);
		if(obj != null && obj instanceof JSONArray){
			JSONArray links = (JSONArray)obj;
			for(int i = 0; i < links.length(); i++){
				JSONObject arrayElement = links.optJSONObject(i);
				if(arrayElement != null && arrayElement.optString("Title").equals(JK_CARDNAME)){
					bpCode = arrayElement.optString("Key");
					break;
				}
			}
		}
		return bpCode;
	}
}
